package UTS.view;

import UTS.controler.PengendaliPeriksaSuratDokumen;
import UTS.model.Dokumen;
import UTS.model.DokumenTableModel;
import UTS.model.Sekretariat;
import UTS.model.Surat;
import UTS.model.SuratTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Vincentius HMK
 */
public class PanelPeriksaSuratDokumen extends JPanel implements ActionListener {
private JLabel labelTanggalAwal;
    private com.toedter.calendar.JDateChooser tanggalAwal;
    private JLabel labelTanggalAkhir;
    private com.toedter.calendar.JDateChooser tanggalAkhir;
    private SuratTableModel suratTableModel;
    private JButton btnLihat;
    private JTable tabelData;
    private JButton btnTerima;
    private JRadioButton radioSurat;
    private JRadioButton radioDokumen;
    private ButtonGroup bgPilihan;
    private DokumenTableModel dokumenTableModel;

    public PanelPeriksaSuratDokumen() {
        setLayout(null);

        radioSurat = new JRadioButton();
        radioSurat.setText("Surat");
        radioSurat.setPreferredSize(new java.awt.Dimension(59, 20));
        radioSurat.setBounds(20, 10, 100, 25);
        this.add(radioSurat);

        radioDokumen = new JRadioButton();
        radioDokumen.setText("Dokumen");
        radioDokumen.setPreferredSize(new java.awt.Dimension(59, 20));
        radioDokumen.setBounds(120, 10, 100, 25);
        this.add(radioDokumen);

        bgPilihan = new ButtonGroup();
        bgPilihan.add(radioSurat);
        bgPilihan.add(radioDokumen);

        labelTanggalAwal = new JLabel();
        labelTanggalAwal.setText("Tanggal Awal");
        labelTanggalAwal.setBounds(20, 45, 100, 25);
        this.add(labelTanggalAwal);

        tanggalAwal = new com.toedter.calendar.JDateChooser();
        tanggalAwal.setBounds(130, 45, 300, 25);
        this.add(tanggalAwal);

        labelTanggalAkhir = new JLabel();
        labelTanggalAkhir.setText("Tanggal Lahir");
        labelTanggalAkhir.setBounds(20, 80, 100, 25);
        this.add(labelTanggalAkhir);

        tanggalAkhir = new com.toedter.calendar.JDateChooser();
        tanggalAkhir.setBounds(130, 80, 300, 25);
        this.add(tanggalAkhir);

        btnLihat = new JButton("Lihat");
        btnLihat.setBounds(20, 115, 100, 25);
        this.add(btnLihat);

        btnLihat.addActionListener(new ActionListener() {



            public void actionPerformed(ActionEvent e) {
                if(radioSurat.isSelected()){
                try {
                    ArrayList<Surat> content = (ArrayList<Surat>) PengendaliPeriksaSuratDokumen.daftarPeriodeSurat(tanggalAwal.getDate(), tanggalAkhir.getDate());
                    suratTableModel = new SuratTableModel(content);
                    tabelData.setModel(suratTableModel);



                } catch (Exception ex) {
                    Logger.getLogger(PanelPeriksaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            else{
                try {
                    ArrayList<Dokumen> content = (ArrayList<Dokumen>) PengendaliPeriksaSuratDokumen.daftarPeriodeDokumen(tanggalAwal.getDate(), tanggalAkhir.getDate());
                    dokumenTableModel = new DokumenTableModel(content);
                    tabelData.setModel(dokumenTableModel);


                } catch (Exception ex) {
                    Logger.getLogger(PanelPeriksaSuratDokumen.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            }
        });

        tabelData = new JTable();
        tabelData.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{
                    "Nomor", "Perihal", "Asal", "Penerima", "Letak", "Tanggal Diterima", "Status"
                }));

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(tabelData);
        scrollPane.setBounds(60, 150, 800, 300);
        this.add(scrollPane);


        btnTerima = new JButton("Terima");
        btnTerima.setBounds(20, 500, 100, 25);
        this.add(btnTerima);

        btnTerima.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
               
                int[] tbl = tabelData.getSelectedRows();

                if(radioSurat.isSelected()){
                for (int i = 0; i < tbl.length; i++) {
                    suratTableModel.getSurat(tbl[i]).setStatusSurat("Diterima");
                    suratTableModel.getSurat(tbl[i]).updateStatusSurat();
                }//System.out.println(tabelDataSurat.getValueAt(tblSrt[0], 6));
                }else{
                   for (int i = 0; i < tbl.length; i++) {
                    dokumenTableModel.getDokumen(tbl[i]).setStatusDokumen("Diterima");
                    dokumenTableModel.getDokumen(tbl[i]).updateStatusDokumen();
                }
                }
                tabelData.repaint();
            }

        });
    }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}

